const path = require('path')
const YamlJson = require('yamljs')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin') /* 提取css到一个文件 */
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader/dist/index')
const config = require('./env')
module.exports = {
  mode: 'development',
  entry: {
    main: './src/main.ts',
  },
  output: {
    filename: 'js/[name]__[hash].js',
    path: path.resolve(__dirname, 'dist'),
    clean: true /* 每次打包清理资源 */,
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        use: ['vue-loader'],
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader'],
      },
      {
        test: /\.s[ac]ss$/i,
        use: [
          'style-loader',
          'css-loader',
          'postcss-loader',
          {
            loader: 'sass-loader',
            options: {
              sassOptions: {
                indentedSyntax: true /* sass采用缩进语法 */,
              },
            },
          },
        ],
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: 'asset/resource' /* webpack内置文件处理 Asset */,
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i,
        type: 'asset/resource' /* webpack内置文件处理 Asset */,
      },
      {
        test: /\.yaml$/,
        type: 'json',
        parser: {
          parse: YamlJson.parse,
        },
      },
      {
        test: /\.js$/,
        exclude: /node_module/,
        loader: 'babel-loader',
      },
      {
        test: /\.ts?$/,
        exclude: /node_modules/,
        use: [
          'babel-loader',
          {
            loader: 'ts-loader',
            options: { appendTsxSuffixTo: [/\.vue$/, /\.ts$/], allowTsInNodeModules: true },
          },
        ],
      },
    ],
  },
  infrastructureLogging:{
    level:"none"
  },
  plugins: [
    new webpack.DefinePlugin({
      config:JSON.stringify(config)
    }),
    new HtmlWebpackPlugin({
      title: 'webpack5 && Vue3' /* title标签<%= %> */,
      filename: 'index.html' /* 生成后的文件名 */,
      template: path.join(__dirname, 'template.html') /* 模板文件 地址 */,
    }),
    new VueLoaderPlugin() /* vue模板编译 */,
    new MiniCssExtractPlugin({ filename: '[hash].css' }),
    new FriendlyErrorsWebpackPlugin()
  ],
  resolve: {
    roots: [__dirname],
    alias: {
      '@': path.resolve('src'),
    },
    extensions: ['.ts', '.tsx', '.js'],
  },
  optimization: {
    // minimize: true,
    minimizer: [
      new CssMinimizerPlugin(),
    ] /* 如果还想在开发环境下启用 CSS 优化，请将 optimization.minimize 设置为 true: */,
  },
  performance: {
    maxAssetSize: 300000000,
  },
  devServer:{
    quiet: true,
  },
  stats: 'errors-only'
}
